package cn.dai.servlet;

import cn.dai.dao.UserDaoImpl;
import cn.dai.dao.UserRoleMapDaoImpl;
import cn.dai.pojo.User;
import cn.dai.service.UserRoleMapService;
import cn.dai.service.UserRoleMapServiceImpl;
import cn.dai.service.UserService;
import cn.dai.service.UserServiceImpl;
import cn.dai.service.model.Page;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ArkD42
 * @file RBAC
 * @create 2020 - 06 - 11 - 12:04
 */

@WebServlet("/user")
public class UserServlet extends BaseServlet{

    UserService userService = new UserServiceImpl(new UserDaoImpl());
    UserRoleMapService userRoleMapService = new UserRoleMapServiceImpl(new UserRoleMapDaoImpl());

    /**
     * 跳转用户列表页面
     * http://localhost:8080/RBAC/user?act=userList&page=1
     * @param request
     * @param response
     * @return
     */
    public String userList(HttpServletRequest request, HttpServletResponse response){
        String p = request.getParameter("page");
        if (p == null) p = "1";
        Integer pageIndex = Integer.valueOf(p);
        Page<User> userPage = userService.getUserPage(pageIndex);
        request.setAttribute("userPage",userPage);
        return "/WEB-INF/admin/user/userList.jsp";
    }


    /**
     * 接受用户列表携带的用户ID值，查询出用户的所有信息，转发给用户修改页
     * @param request
     * @param response
     * @return
     */
    public String toEditUser(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        User user = userService.getUserById(Integer.valueOf(id));
        request.setAttribute("user",user);
        return "/WEB-INF/admin/user/userEdit.jsp";
    }

    /**
     * 接受从User修改页发送的表单信息，调用更新SQL，之后重定向到用户列表
     * @param request
     * @param response
     */
    public void editUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = new User();
        user.setU_id( Integer.valueOf(request.getParameter("id")) );
        user.setU_name( request.getParameter("username"));
        user.setU_password( request.getParameter("password") );
        user.setU_email( request.getParameter("email") );
        user.setU_phone( request.getParameter("phone") );
        user.setU_status( Integer.valueOf(request.getParameter("status")) );
        int i = userService.editUser(user);
        System.out.println("更新User操作影响结果：" + i);
        response.sendRedirect(request.getContextPath() + "/user?act=userList");
    }

    /**
     * 接受用户列表发送的删除请求，获取要删除的用户ID，执行SQL删除，重定向回用户列表
     * @param request
     * @param response
     */
    public void toDeleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int i = userService.delUser(Integer.valueOf(id));
        System.out.println("删除User操作影响结果：" + i);
        response.sendRedirect(request.getContextPath() + "/user?act=userList");
    }

    /**
     * 访问WEB-INF内的资源，你就是需要我Servlet来转发，对卟住，是真的可以为所欲为.jpg
     * 从用户列表发送的请求，需要跳转到添加页面来，而且重定向也不行，重定向也不能访问进来我日。。。。
     * @param request
     * @param response
     */
    public String toAddUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "/WEB-INF/admin/user/userAdd.jsp";
    }

    /**
     * 从添加用户页面接受表单信息，注入到SQL插入执行，重定向回到用户列表
     * @param request
     * @param response
     */
    public void forAddUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = new User();
        user.setU_name( request.getParameter("username"));
        user.setU_password( request.getParameter("password") );
        user.setU_email( request.getParameter("email") );
        user.setU_phone( request.getParameter("phone") );
        user.setU_status( Integer.valueOf(request.getParameter("status")) );
        int i = userService.addUser(user);
        System.out.println("添加User操作影响结果：" + i);
        response.sendRedirect(request.getContextPath() + "/user?act=userList");
    }

    /**
     * 用户角色权限设置
     * @param request
     * @param response
     * @return
     */
    public String toUserRoleAssociate(HttpServletRequest request, HttpServletResponse response){

        return "";
    }

    /**
     * 用于批量删除用户，根据前端传入的请求参数数组来完成执行
     * @param request
     * @param response
     * @return
     */
    public String toDelUserByIds(HttpServletRequest request, HttpServletResponse response){

        String[] theDelIds = request.getParameterValues("theDelIds");

        int i = userService.delUserByUidArray(theDelIds);

        System.out.println("一共批量删除了 " + i + " 个用户");

        return "redirect:/user?act=userList";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     */
    public String toSearchUsers(HttpServletRequest request, HttpServletResponse response){
        String keywords = request.getParameter("keywords");
        if (keywords == null || "".equals(keywords)) return "redirect:/user?act=userList";
        List<User> searchResults = userService.getUsersBySearch(keywords);
        request.setAttribute("resultList",searchResults);
        return "forward:/WEB-INF/admin/user/userSearchResult.jsp";
    }


}
